package com.ruoyi.system.service.impl;

import com.ruoyi.system.mapper.AgoniEventMapper;
import com.ruoyi.system.mapper.AgoniUserMapper;
import com.ruoyi.system.pojo.entity.Event;
import com.ruoyi.system.pojo.entity.User;
import com.ruoyi.system.pojo.vo.AllConditionEventVO;
import com.ruoyi.system.pojo.vo.MyPublishEventVO;
import com.ruoyi.system.service.IAgoniEventService;
import com.ruoyi.system.service.IAgoniUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IAgoniEventServiceImpl implements IAgoniEventService {
    @Autowired
    private AgoniEventMapper eventMapper;
    @Autowired
    private AgoniUserMapper userMapper;

    /**
     * 查询所有事件
     **/
    @Override
    public List<Event> listEvents() {
        return eventMapper.listEvents();
    }


    /**
     * 根据设施ID查询事件
     * @param facilityId 事件ID
     * @return 事件
     */
    @Override
    public List<Event> getEventsByFacilityId(Long facilityId) {
        return eventMapper.getEventsByFacilityId(facilityId);
    }


    /**
     * 查询事件更多信息
     */
    @Override
    public List<AllConditionEventVO> listAllConditionEvents() {
        List<Event> events = eventMapper.listEvents();

        List<AllConditionEventVO> allConditionEventVOS = new ArrayList<>();

        //对于每一个事件，查询对应的发布者信息
        for (Event event : events) {
            //取出发布者ID
            Long publisherId = Long.valueOf(event.getCreatedBy());
            //根据发布者ID查询发布者信息
            User publisher = userMapper.selectUserById(publisherId);
            AllConditionEventVO allConditionEventVO = AllConditionEventVO.builder()
                    .id(event.getId())
                    .title(event.getTitle())
                    .eventType(event.getEventType())
                    .createdBy(publisher.getId())
                    .description(event.getDescription())
                    .eventDate(event.getEventDate())
                    .facilityId(event.getFacilityId())
                    .location(event.getLocation())
                    .reward(event.getReward())
                    .receiverId(event.getReceiverId())
                    .status(event.getStatus())
                    .createdByPhone(publisher.getEmail())
                    .createdByName(publisher.getUsername())
                    .build();
            allConditionEventVOS.add(allConditionEventVO);
        }



        return allConditionEventVOS;
    }


    @Override
    public int addEvent(Event event, Long userId) {
        Event newEvent = new Event();
        newEvent.setTitle(event.getTitle());
        newEvent.setEventType(event.getEventType());
        newEvent.setDescription(event.getDescription());
        newEvent.setEventDate(event.getEndDate());
        newEvent.setFacilityId(event.getFacilityId());
        newEvent.setLocation(event.getLocation());
        newEvent.setEndDate(event.getEndDate());
        newEvent.setStartDate(event.getStartDate());
        newEvent.setLocation(event.getLocation());
        newEvent.setCreatedBy(Math.toIntExact(userId));
        newEvent.setReward(event.getReward());
        newEvent.setStatus(1);
        int result = eventMapper.insertEvent(newEvent);

        return 0;
    }



    //用户接取任务列表

    @Override
    public List<AllConditionEventVO> getEventByReceiverId(Long receiverId) {
        List<Event> events = eventMapper.listEventsByReceiverId(receiverId);
        List<AllConditionEventVO> allConditionEventVOS = new ArrayList<>();

        //对于每一个事件，查询对应的发布者信息
        for (Event event : events) {
            //取出发布者ID
            Long publisherId = Long.valueOf(event.getCreatedBy());
            //根据发布者ID查询发布者信息
            User publisher = userMapper.selectUserById(publisherId);
            AllConditionEventVO allConditionEventVO = AllConditionEventVO.builder()
                    .id(event.getId())
                    .title(event.getTitle())
                    .eventType(event.getEventType())
                    .createdBy(publisher.getId())
                    .description(event.getDescription())
                    .eventDate(event.getEventDate())
                    .facilityId(event.getFacilityId())
                    .location(event.getLocation())
                    .receiverId(event.getReceiverId())
                    .status(event.getStatus())
                    .createdByPhone(publisher.getPhone())
                    .createdByName(publisher.getUsername())
                    .endDate(event.getEndDate())
                    .startDate(event.getStartDate())
                    .reward(event.getReward())
                    .build();
            allConditionEventVOS.add(allConditionEventVO);
        }
        return allConditionEventVOS;
    }

    //接收者接取任务


    @Override
    public int updateReceiveEvent(Long eventId, Long userId) {
        log.info("eventId:{},userId:{}", eventId, userId);
        int result = eventMapper.updateReceiveEvent(eventId, userId);
        System.out.println("result:" + result);
        if (result == 1) {
            return result;
        }
        return 0;
    }

    /**
     * 用户端查询自己发布的事件
     * @param userId 用户ID
     */
    @Override
    public List<MyPublishEventVO> getMyPublishEventList(Long userId) {
        List<Event> events = eventMapper.listEventsByPublisherId(userId);
        List<MyPublishEventVO> myPublishEventVOList = new ArrayList<>();
        for (Event event : events) {
//            MyPublishEventVO myPublishEventVO=new MyPublishEventVO();
            //status1：已发布，2：被接收，3：被完成，4：雇主确认，5：完成事件)
            User receiver=new User();
            if(event.getStatus()!=1){
               receiver=userMapper.selectUserById(Long.valueOf(event.getReceiverId()));
            }
            MyPublishEventVO myPublishEventVO=MyPublishEventVO.builder()
                    .id(event.getId())
                    .title(event.getTitle())
                    .eventType(event.getEventType())
//                    .createdBy(publisher.getId())
                    .description(event.getDescription())
                    .eventDate(event.getEventDate())
                    .facilityId(event.getFacilityId())
                    .location(event.getLocation())
                    .receiverId(event.getReceiverId())
                    .status(event.getStatus())
//                    .createdByPhone(publisher.getPhone())
//                    .createdByName(publisher.getUsername())
                    .endDate(event.getEndDate())
                    .startDate(event.getStartDate())
                    .reward(event.getReward())
                    .receiverName(receiver.getUsername())
                    .receiverPhone(receiver.getPhone())
                    .build();
            myPublishEventVOList.add(myPublishEventVO);

        }
        return myPublishEventVOList;

    }
    /**
     * 接取任务完成事件
     * @param eventId 事件ID
     */
    @Override
    public int completeEvent(Long eventId) {
        int result=0;
         result = eventMapper.completeEvent(eventId);
        return result;
    }
    /**
     * 除开自己以外发布的事件
     * @param userId 用户ID
     */
    @Override
    public List<AllConditionEventVO> listAllConditionEventsWithoutSelf(Long userId) {
        List<Event> events = eventMapper.listEventsWithoutSelf(userId);
        List<AllConditionEventVO> allConditionEventVOS = new ArrayList<>();

        //对于每一个事件，查询对应的发布者信息
        for (Event event : events) {
            //取出发布者ID
            Long publisherId = Long.valueOf(event.getCreatedBy());
            //根据发布者ID查询发布者信息
            User publisher = userMapper.selectUserById(publisherId);
            AllConditionEventVO allConditionEventVO = AllConditionEventVO.builder()
                    .id(event.getId())
                    .title(event.getTitle())
                    .eventType(event.getEventType())
                    .createdBy(publisher.getId())
                    .description(event.getDescription())
                    .eventDate(event.getEventDate())
                    .facilityId(event.getFacilityId())
                    .location(event.getLocation())
                    .receiverId(event.getReceiverId())
                    .status(event.getStatus())
                    .createdByPhone(publisher.getPhone())
                    .createdByName(publisher.getUsername())
                    .endDate(event.getEndDate())
                    .startDate(event.getStartDate())
                    .reward(event.getReward())
                    .build();
            allConditionEventVOS.add(allConditionEventVO);
        }
        return allConditionEventVOS;
    }
}
